package com.sctvcloud.renhe.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.beans.EarnIntegralRule;
import com.sctvcloud.renhe.ui.utils.GlideUtil;
import com.shuyu.gsyvideoplayer.widget.BarrageListAdapter;

import java.util.ArrayList;
import java.util.List;

public class EarnIntegralAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //1:签到;2:评论;3:转发;4:投稿;5:采用投稿;6:邀请码分享;7:新用户注册;8:输入邀请码
    public static final String SIGN_IN = "1";
    public static final String COMMENT ="2";
    public static final String FORWARD = "3";
    public static final String SUBMISSION = "4";
    public static final String USE_SUBMISSION = "5";
    public static final String SHARE_INVITATION = "6";
    public static final String REGISTER = "7";
    public static final String INPUT_INVITATION = "8";

    private List<EarnIntegralRule> ruleList = new ArrayList<>();
    private Context ctx;
    private OnItemBtnClick listener;

    public EarnIntegralAdapter(Context ctx, List<EarnIntegralRule> lList, OnItemBtnClick listener) {
        this.ctx = ctx;
        this.ruleList = lList;
        this.listener = listener;
    }

    static class RuleViewHolder extends RecyclerView.ViewHolder {
        ImageView ruleImage;
        CustomFontTextView ruleTitle;
        CustomFontTextView ruleContent;
        CustomFontTextView toDo;

        public RuleViewHolder(View view) {
            super(view);
            ruleImage = (ImageView) view.findViewById(R.id.rule_image);
            ruleTitle = (CustomFontTextView) view.findViewById(R.id.rule_title);
            ruleContent = (CustomFontTextView) view.findViewById(R.id.rule_content);
            toDo = (CustomFontTextView) view.findViewById(R.id.rule_to_do);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.earn_integral_list_item, parent, false);
        holder = new RuleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != ruleList && ruleList.size() > position) {
            final EarnIntegralRule rule = ruleList.get(position);
            RuleViewHolder h = (RuleViewHolder) holder;

            h.toDo.setVisibility(View.VISIBLE);
            if (SIGN_IN.equalsIgnoreCase(rule.getRuleType())) {
                h.toDo.setText("去签到");
            } else if (SHARE_INVITATION.equalsIgnoreCase( rule.getRuleType()) ) {
                h.toDo.setText("去分享");
            } else {
                h.toDo.setVisibility(View.GONE);
            }
            h.toDo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onItemBtnClick(rule.getRuleType());
                    }
                }
            });

            GlideUtil.loadImgWith34DefNormal(ctx, rule.getRuleIcon(), h.ruleImage);
            h.ruleTitle.setText(rule.getRuleName());
            h.ruleContent.setText(setNumColor(rule.getRuleNote()));
        }
    }

    public void setDataList(List<EarnIntegralRule> list) {
        this.ruleList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null != ruleList) {
            return ruleList.size();
        } else {
            return 0;
        }
    }

    public SpannableStringBuilder setNumColor(String str) {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        for (int i = 0; i < str.length(); i++) {
            char a = str.charAt(i);
            if (a >= '0' && a <= '9') {
                style.setSpan(new ForegroundColorSpan(ctx.getResources().getColor(R.color.blue_main)), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return style;
    }

    public interface OnItemBtnClick {
        void onItemBtnClick(String type);
    }
}

